<template>

  <el-card style="width:40%; margin-left: 20px ; padding: 50px">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="新密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-check" type="primary" @click="submitForm('ruleForm')">确认修改</el-button>
        <el-button icon="el-icon-refresh-right" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "SchoolUserChangePass",
  data(){
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        id:'',
        username:'',
        schoolname:'',
        password: '',
        checkPass: '',
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' },
          { min:6, message:'长度不能少于6个字符', trigger: 'blur'}
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.id=this.$store.state.schoolId,
          this.ruleForm.username=this.$store.state.schooluser.username,
          this.ruleForm.schoolname=this.$store.state.schoolname,
          delete this.ruleForm.checkPass
          axios.put("http://localhost:8081/schooluser/update",this.ruleForm).then((res)=>{
            if (res.data.msg==="更新成功"){
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              //添加成功后置空
              this.resetForm('ruleForm')
            }
            else{
              this.$message({
                message: '修改失败',
                type: 'fail'
              });
            }
          })

        } else {
          console.log('提交失败!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-input{
  width: 60%;
}
</style>
